package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
	"time"
)

// 使用chan来模拟mq

var chanList chan string

func producer(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello golang http!")
	go func() {
		//往队列里写入任务（生产者）
		chanList <- "hello"
	}()
}

func main() {
	chanList = make(chan string, 5)
	go consumer()
	go consumer()
	go consumer()

	http.HandleFunc("/", producer)
	http.ListenAndServe(":8001", nil)
}

func consumer() {
	for {
		// 从队列里取出任务（消费者）
		fmt.Println(<-chanList)
		time.Sleep(time.Microsecond * 1000)
	}
}
